package com.simpleplayer.services;

import android.os.Handler;

import androidx.annotation.NonNull;

//自实现的html提取类
public class CustomHTMLResourceExtractor extends HTMLResourceExtractor{

    private static final String TAG = "CustomHTMLResourceExtractor";

    private static final String EXTRACT_THREAD_NAME = "custom-html-thread";

    protected final Listener mListener;
    protected final String mUrl; // 要提取的网址
    private final Handler mMainHandler;

    public CustomHTMLResourceExtractor(@NonNull String url, Listener listener) {
        mListener = listener;
        mUrl = url;
        this.mMainHandler = new Handler();

        // 启动一个子线程请求网页 html 内容
        new Thread(() -> {
            String content = NetWorkService.getURLContent(url);
            if (content != null) {
                mMainHandler.post(()-> {
                    onGetURLSource(content);
                });
            }
        }, EXTRACT_THREAD_NAME).start();
    }

    private void onGetURLSource(String content) {
        if (mListener == null) return;

        if (content == null || content.isEmpty()) {
            mListener.onExtractFinish(this, null);
        } else {
            parseContent();
        }
    }

    private void parseContent() {

    }
}
